package com.neutech.neupha.controller;

import java.io.File;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.neutech.neupha.service.PhaService;
import com.neutech.neupha.vo.ToAddUserVo;
import com.neutech.neupha.vo.ViewDate;
import com.neutech.po.Drug;
import com.neutech.po.Neupha;





@RestController

@RequestMapping("/his")
public class PhaController {
	@Autowired
	private PhaService phaService;

	@RequestMapping(value="addNewMage",method=RequestMethod.POST)
	   public Drug addNewMage(@RequestBody Map<String,Object> drug) {
		   
		   phaService.addNewDrug(drug);
		   Drug redrug = new Drug(); 
		   
		   return redrug;
	   }
	 @RequestMapping("/delDrug")
	 public @ResponseBody String delUserById(Integer id) {
		int result = phaService.delDrug(id);
	
		if(result>=1) {
			return "你太行了";
		}
		return "完了老弟";
	 }
	   @RequestMapping("/getDrugs")
	   public ViewDate getDrugs(@RequestParam Map<String,Object> map ){
		   return phaService.getDrugs(map);
	   }
	   @RequestMapping(value="/updateDrug",method=RequestMethod.POST)
		public String updateDrug(@RequestBody Drug drug){
			int result = phaService.updateDrug(drug);
			System.out.println(drug.getDrugsCode());
			System.out.println(drug.getId());
			if(result>0){
				return "成功";
			}else{
				return "失败";
			}
		}
	   @CrossOrigin
	   @RequestMapping(value="/upExcel")
	   public String upExcel(@RequestParam MultipartFile file,HttpServletRequest request) throws Exception{
		   ServletContext context = request.getServletContext();
		   String basePath = context.getRealPath("/uploads");
		   file.transferTo(new File(basePath,file.getOriginalFilename()));
	       System.out.println("已经将上传文件保存到："+basePath+file.getOriginalFilename());
		   return "success";
	   }
}
